package com.example.a2205douyin;

import android.animation.ValueAnimator;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyView extends View {
    private Paint paint;//画笔
    int color = Color.RED;
    int width = 5;

    int angle;
    int time;
    public MyView(Context context) {
        super(context);
        initPaint();
    }
    //实例化画笔，并设置画笔属性
    private void initPaint() {
        paint = new Paint();
        paint.setColor(color);//设置画笔颜色
        paint.setStrokeWidth(width);//画笔的宽度---画笔的粗细
        paint.setAntiAlias(true);//抗锯齿化  让图形更圆滑
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextSize(50);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        color = array.getColor(R.styleable.MyView_my_color, color);
        width = array.getInt(R.styleable.MyView_my_width,width);
        array.recycle();

        initPaint();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPaint();
    }
    //Canvas 画布---纸
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //500 500  代表文字开始的地方距左500 距上500
       /* canvas.drawText("张三",500,500,paint);
        canvas.drawLine(500,500,1000,500,paint);
        canvas.drawLine(500,500,500,1000,paint);
        canvas.drawLine(1000,500,1000,1000,paint);
        canvas.drawLine(500,1000,1000,1000,paint);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(750,750,250,paint);
        canvas.drawLine(0,0,500,0,paint);
        canvas.drawLine(0,0,0,500,paint);
        canvas.drawLine(500,0,500,500,paint);
        canvas.drawLine(0,500,500,500,paint);
        RectF f = new RectF(0, 0, 500, 500);//矩形 左边的线距左   上边的线距上   右边的线距左  下边的线距上
        canvas.drawArc(f,0,180,true,paint);*/
        RectF f = new RectF(0, 0, 500, 500);//矩形 左边的线距左   上边的线距上   右边的线距左  下边的线距上
        canvas.drawArc(f,0,angle,false,paint);//0 代表开始的弧度角度 180 代表的是扫过的角度
        canvas.drawText(time+"秒",230,260,paint);
    }

    public void start(int second){
        time = second;//倒计时时间
        {
            ValueAnimator animator = ValueAnimator.ofInt(second, 0);
            animator.setDuration(second*1000);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    time = (int) animation.getAnimatedValue();
                    invalidate();//重新绘制
                    if(time==0){
                        MyApp.getContext().startActivity(new Intent(MyApp.getContext(),DouyinActivity.class));
                    }
                }
            });
            animator.start();

        }
        {
            ValueAnimator animator = ValueAnimator.ofInt(0, 360);
            animator.setDuration(second*1000);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    angle = (int) animation.getAnimatedValue();
                    invalidate();
                }
            });
            animator.start();
        }
    }
    //测量，显示
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int size1 = MeasureSpec.getSize(heightMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);//获取控件宽度的测量模式
        int mode1 = MeasureSpec.getMode(heightMeasureSpec);
        if(mode==MeasureSpec.AT_MOST){
            size = 510;
        }
        if(mode1==MeasureSpec.AT_MOST){
            size1 = 510;
        }

        setMeasuredDimension(size,size1);
    }
}
